using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PietschSoft.VE;

namespace SecurityLinkUp
{
    public partial class ViewMeeting : System.Web.UI.Page
    {
        private Guid MeetingID;

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void LoadData()
        {
            // get the meeting ID from the querystring
            MeetingID = new Guid(Page.Request.QueryString["MeetingID"]);

            // lookup the meeting
            Meeting m = MeetingData.GetMeeting(MeetingID);

            // Check we have a valid meeting
            if (m == null)
            {
                return;
            }

            // populate the details
            User u = UserData.GetUser(m.OrganizerID);
            Organizer.Text = u.name;
            MeetingTitle.Text = m.Title;
            Start.Text = m.Start.ToShortDateString() + " " + m.Start.ToShortTimeString();
            End.Text = m.End.ToShortDateString() + " " + m.End.ToShortTimeString();
            Description.Text = m.Description;
            location.Text = m.Location;
            if (m.HasPosition)
            {
                LatLong Position = new LatLong(m.geolat, m.geolong);
                Pushpin p = new PietschSoft.VE.Pushpin(m.uid.ToString(), Position, null, m.Location, m.AdditionalInfo);
                Map.Pushpins.Add(p);
                Map.Latitude = m.geolat;
                Map.Longitude = m.geolong;
                Map.Zoom = 15;
            }

            ArrayList attendees = MeetingData.GetAllMeetingRSVPs(MeetingID);
            DrawAttendeeList(attendees);

            // display currently accepted presentations for this meeting
            ArrayList presentations = MeetingData.GetMeetingPresentations(MeetingID);
            DrawPresentationList(presentations);


            // if the meeting is not in the past, show the "help me get to this meeting" panel
            if (m.End > DateTime.Now)
            {
                LocationPanel.Visible = true;
            }
            else
            {
                LocationPanel.Visible = false;
            }

            // if the user is logged in, see if we can show the RSVP and presentation panels
            MembershipUser usr = Membership.GetUser();
            if (usr != null)
            {
                // only show the panel if the meeting is not in the past
                if (m.End > DateTime.Now)
                {
                    RSVPPanel.Visible = true;

                    // get the user RSVP info, if there is any
                    RSVP r = MeetingData.GetRSVP(MeetingID, (Guid)usr.ProviderUserKey);
                    if (r != null)
                    {
                        // set the info so they can update it if necessary
                        switch (r.status)
                        {
                            case RSVP.RSVPStatus.ATTENDING:
                                WillAttendRB.Checked = true;
                                WillNotAttendRB.Checked = false;
                                UndecidedRB.Checked = false;
                                break;
                            case RSVP.RSVPStatus.NOTATTENDING:
                                WillAttendRB.Checked = false;
                                WillNotAttendRB.Checked = true;
                                UndecidedRB.Checked = false;
                                break;
                            case RSVP.RSVPStatus.UNSURE:
                                WillAttendRB.Checked = false;
                                WillNotAttendRB.Checked = false;
                                UndecidedRB.Checked = true;
                                break;
                        }
                        RSVPMessage.Text = r.message;
                    }

                    // see if user has uploaded a presentation
                    if (!UserHasPresentation((Guid)usr.ProviderUserKey))
                    {
                        // show the upload presentation panel
                        UploadPresentationPanel.Visible = true;
                    }
                }
            }
        }

        protected bool UserHasPresentation(Guid userid)
        {
            ArrayList userpresentations = MeetingData.GetUserPresentations(userid);
            foreach (Presentation p in userpresentations)
            {
                if (p.userid == userid)
                {
                    return true;
                }
            }
            return false;

        }

        protected void DrawAttendeeList(ArrayList attendees)
        {
            Table tbl = new Table();
            TableHeaderRow thr = new TableHeaderRow();
            TableHeaderCell nameheader = new TableHeaderCell();
            nameheader.Text = "Name";
            thr.Cells.Add(nameheader);
            TableHeaderCell statusheader = new TableHeaderCell();
            statusheader.Text = "Status";
            thr.Cells.Add(statusheader);
            TableHeaderCell messageheader = new TableHeaderCell();
            messageheader.Text = "Message";
            thr.Cells.Add(messageheader);
            tbl.Rows.Add(thr);

            foreach (RSVP r in attendees)
            {
                User usr = UserData.GetUser(r.UserID);
                string status = "";
                switch (r.status)
                {
                    case RSVP.RSVPStatus.ATTENDING:
                        status = "Will be attending";
                        break;
                    case RSVP.RSVPStatus.NOTATTENDING:
                        status = "Will not be attending";
                        break;
                    case RSVP.RSVPStatus.UNSURE:
                        status = "Not sure yet";
                        break;
                }

                TableRow tr = new TableRow();
                TableCell namecell = new TableCell();
                namecell.Text = usr.name;
                namecell.ID = "attendee-" + usr.uid.ToString();
                namecell.CssClass = "popuplink";
                namecell.Attributes.Add("OnFocus", "ShowPopup('ctl00_ContentPlaceHolder_attendee-" + usr.uid + "')");
                tr.Cells.Add(namecell);
                TableCell statuscell = new TableCell();
                statuscell.Text = status;
                tr.Cells.Add(statuscell);
                TableCell messagecell = new TableCell();
                messagecell.Text = r.message;
                tr.Cells.Add(messagecell);
                tbl.Rows.Add(tr);
            }

            AttendeesPlaceholder.Controls.Add(tbl);
        }

        protected void DrawPresentationList(ArrayList presentations)
        {
            Table tbl = new Table();
            TableHeaderRow thr = new TableHeaderRow();
            TableHeaderCell speakerheader = new TableHeaderCell();
            speakerheader.Text = "Speaker";
            thr.Cells.Add(speakerheader);
            TableHeaderCell titleheader = new TableHeaderCell();
            titleheader.Text = "Title";
            thr.Cells.Add(titleheader);
            TableHeaderCell durationheader = new TableHeaderCell();
            durationheader.Text = "Duration";
            thr.Cells.Add(durationheader);
            TableHeaderCell linkheader = new TableHeaderCell();
            linkheader.Text = "Link";
            thr.Cells.Add(linkheader);
            tbl.Rows.Add(thr);

            foreach (Presentation p in presentations)
            {
                // check to see if the presentation is accepted
                if (p.accepted == true)
                {
                    User usr = UserData.GetUser(p.userid);

                    TableRow tr = new TableRow();
                    TableCell speakercell = new TableCell();
                    speakercell.Text = usr.name;
                    speakercell.ID = "speaker-" + usr.uid.ToString();
                    speakercell.CssClass = "popuplink";
                    speakercell.Attributes.Add("OnFocus", "ShowPopup('ctl00_ContentPlaceHolder_speaker-" + usr.uid + "')");
                    tr.Cells.Add(speakercell);
                    TableCell titlecell = new TableCell();
                    titlecell.Text = p.title;
                    tr.Cells.Add(titlecell);
                    TableCell durationcell = new TableCell();
                    durationcell.Text = p.estimatedduration;
                    tr.Cells.Add(durationcell);
                    TableCell linkcell = new TableCell();
                    if (p.HasSlidedeck)
                    {
                        HyperLink link = new HyperLink();
                        link.Text = "Download slidedeck";
                        link.NavigateUrl = p.slidedeckurl;
                        link.Target = "_Blank";
                        linkcell.Controls.Add(link);
                    }
                    tr.Cells.Add(linkcell);
                    tbl.Rows.Add(tr);
                }
            }
            PresentationsPlaceHolder.Controls.Add(tbl);
        }


        protected void SaveRSVPBtn_Click(object sender, EventArgs e)
        {
            MembershipUser usr = Membership.GetUser();
            if (usr != null)
            {
                // get the status selected
                RSVP.RSVPStatus status = RSVP.RSVPStatus.NOANSWER;
                if (WillAttendRB.Checked == true)
                {
                    status = RSVP.RSVPStatus.ATTENDING;
                }
                if (WillNotAttendRB.Checked == true)
                {
                    status = RSVP.RSVPStatus.NOTATTENDING;
                }
                if (UndecidedRB.Checked == true)
                {
                    status = RSVP.RSVPStatus.UNSURE;
                }
                RSVP r = new RSVP(MeetingID, (Guid)usr.ProviderUserKey, status, RSVPMessage.Text);
                RSVPErrorMessage.Text = MeetingData.SaveRSVPToDB(r);

            }
        }

        protected void SubmitPresentationBtn_Click(object sender, EventArgs e)
        {
            // setup some vars
            Guid guid = Guid.NewGuid();
            MembershipUser usr = Membership.GetUser();

            // bail out if cant find user
            if (usr == null)
            {
                SubmitPresentationErrorMessage.Text = "Invalid user";
                return;
            }

            // create a presentation first
            Presentation p = new Presentation(guid, MeetingID, (Guid)usr.ProviderUserKey, PresentationTitle.Text, Abstract.Text, Duration.SelectedValue, null, false, "", -1);
           
            // save the upload file if there is one
            if (PresentationFile.HasFile)
            {
                string error = p.SavePresentationFile(PresentationFile.PostedFile);

                if (error != "Saved")
                {
                    SubmitPresentationErrorMessage.Text = error;
                    return;
                }
            }

            // save to database
            string error2 = MeetingData.SavePresentationToDB(p);
            SubmitPresentationErrorMessage.Text = error2;

            // if no error, then hide the upload panel as user has already submited
            // a presentation
            if (error2 == "Saved")
            {
                UploadPresentationPanel.Visible = false;
            }
        
        }
    }
}
